/**
 * 
 */
package org.lookouster.nextAgadir.pointageBureau.bases;

import java.util.Collection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.lookouster.nextAgadir.pointageBureau.dbDESC.CTLE;
import org.lookouster.nextAgadir.pointageBureau.dbDESC.Tables;
import org.lookouster.nextAgadir.pointageBureau.outils.BDConnect;
import org.lookouster.nextAgadir.pointageBureau.outils.BDFunctions;
import org.lookouster.nextAgadir.pointageBureau.outils.Commons;
import org.lookouster.nextAgadir.pointageBureau.outils.functions;

/**
 * @author Lookouster
 *
 */
public class Controle {
private int idControle;

	/*
 * (non-javadoc)
 */
private Controleur controleur;
private Date dateControle;
private boolean done=Commons.DB_CTL_NON_TRA;
 

public Controle(Controleur c, List<EspaceTravail> champs)
{
	this.setControleur(c);
	this.setEspaceTravail(champs);
	
}

public Controle(int id)
{
	if(id==0)
		return;
	idControle=id;
	done=false;
}

public void setDone(BDConnect bdc)
{
	done=true;
	try {
		String sql="update "+Tables.DB_TABLE_CTLE+" set "+CTLE.CHP_TRAIT+"="+done
		+" where "+CTLE.CHP_ID+"="+idControle;
		bdc.update(sql);
		System.out.println(sql);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

public Controle()
{
	
}

public Controle(int id, java.sql.Date date)
{
	setIdControle(id);
	setDateControle(date);
}
/**
 * Getter of the property <tt>controleur</tt>
 *
 * @return Returns the controleur.
 * 
 */
public Controleur getControleur()
{
	return controleur;
}



/**
 * Setter of the property <tt>controleur</tt>
 *
 * @param controleur The controleur to set.
 *
 */
public void setControleur(Controleur controleur ){
	this.controleur = controleur;
}

	/**
 *
 */
private Collection<Remarque> remarque;

/**
 * Getter of the property <tt>remarque</tt>
 *
 * @return Returns the remarque.
 * 
 */
public Collection<Remarque> getRemarque()
{
	return remarque;
}
 
/**
 * Returns an iterator over the elements in this collection. 
 *
 * @return an <tt>Iterator</tt> over the elements in this collection
 * @see	java.util.Collection#iterator()
 * 
 */
public Iterator<Remarque> remarqueIterator(){
	return remarque.iterator();
}

/**
 * Returns <tt>true</tt> if this collection contains no elements.
 *
 * @return <tt>true</tt> if this collection contains no elements
 * @see	java.util.Collection#isEmpty()
 *
 */
public boolean isRemarqueEmpty(){
	return remarque.isEmpty();
}

/**
 * Returns <tt>true</tt> if this collection contains the specified element. 
 *
 * @param element whose presence in this collection is to be tested.
 * @see	java.util.Collection#contains(Object)
 *
 */
public boolean containsRemarque(Remarque remarque){
	return this.remarque.contains(remarque);
}

/**
 * Returns <tt>true</tt> if this collection contains all of the elements
 * in the specified collection.
 *
 * @param elements collection to be checked for containment in this collection.
 * @see	java.util.Collection#containsAll(Collection)
 *
 */
public boolean containsAllRemarque(Collection<Remarque> remarque){
	return this.remarque.containsAll(remarque);
}

/**
 * Returns the number of elements in this collection.
 *
 * @return the number of elements in this collection
 * @see	java.util.Collection#size()
 *
 */
public int remarqueSize(){
	return remarque.size();
}

/**
 * Returns all elements of this collection in an array.
 *
 * @return an array containing all of the elements in this collection
 * @see	java.util.Collection#toArray()
 *
 */
public Remarque[] remarqueToArray(){
	return remarque.toArray(new Remarque[remarque.size()]);
}


/**
 * Ensures that this collection contains the specified element (optional
 * operation). 
 *
 * @param element whose presence in this collection is to be ensured.
 * @see	java.util.Collection#add(Object)
 *
 */
public boolean addRemarque(Remarque remarque){
	return this.remarque.add(remarque);
}

/**
 * Setter of the property <tt>remarque</tt>
 *
 * @param remarque the remarque to set.
 *
 */
public void setRemarque(Collection<Remarque> remarque){
	this.remarque = remarque;
}

/**
 * Removes a single instance of the specified element from this
 * collection, if it is present (optional operation).
 *
 * @param element to be removed from this collection, if present.
 * @see	java.util.Collection#add(Object)
 *
 */
public boolean removeRemarque(Remarque remarque){
	return this.remarque.remove(remarque);
}

/**
 * Removes all of the elements from this collection (optional operation).
 *
 * @see	java.util.Collection#clear()
 *
 */
public void clearRemarque(){
	this.remarque.clear();
}

	/**
 *
 */
private Collection<EspaceTravail> espaceTravail;

/**
 * Getter of the property <tt>espaceTravail</tt>
 *
 * @return Returns the espaceTravail.
 * 
 */
public Collection<EspaceTravail> getEspaceTravail()
{
	return espaceTravail;
}
 
/**
 * Returns an iterator over the elements in this collection. 
 *
 * @return an <tt>Iterator</tt> over the elements in this collection
 * @see	java.util.Collection#iterator()
 * 
 */
public Iterator<EspaceTravail> espaceTravailIterator(){
	return espaceTravail.iterator();
}

/**
 * Returns <tt>true</tt> if this collection contains no elements.
 *
 * @return <tt>true</tt> if this collection contains no elements
 * @see	java.util.Collection#isEmpty()
 *
 */
public boolean isEspaceTravailEmpty(){
	return espaceTravail.isEmpty();
}

/**
 * Returns <tt>true</tt> if this collection contains the specified element. 
 *
 * @param element whose presence in this collection is to be tested.
 * @see	java.util.Collection#contains(Object)
 *
 */
public boolean containsEspaceTravail(EspaceTravail espaceTravail){
	return this.espaceTravail.contains(espaceTravail);
}

/**
 * Returns <tt>true</tt> if this collection contains all of the elements
 * in the specified collection.
 *
 * @param elements collection to be checked for containment in this collection.
 * @see	java.util.Collection#containsAll(Collection)
 *
 */
public boolean containsAllEspaceTravail(Collection<EspaceTravail> espaceTravail){
	return this.espaceTravail.containsAll(espaceTravail);
}

/**
 * Returns the number of elements in this collection.
 *
 * @return the number of elements in this collection
 * @see	java.util.Collection#size()
 *
 */
public int espaceTravailSize(){
	return espaceTravail.size();
}

/**
 * Returns all elements of this collection in an array.
 *
 * @return an array containing all of the elements in this collection
 * @see	java.util.Collection#toArray()
 *
 */
public EspaceTravail[] espaceTravailToArray(){
	return espaceTravail.toArray(new EspaceTravail[espaceTravail.size()]);
}


/**
 * Ensures that this collection contains the specified element (optional
 * operation). 
 *
 * @param element whose presence in this collection is to be ensured.
 * @see	java.util.Collection#add(Object)
 *
 */
public boolean addEspaceTravail(EspaceTravail espaceTravail){
	return this.espaceTravail.add(espaceTravail);
}

/**
 * Setter of the property <tt>espaceTravail</tt>
 *
 * @param espaceTravail the espaceTravail to set.
 *
 */
public void setEspaceTravail(Collection<EspaceTravail> espaceTravail){
	this.espaceTravail = espaceTravail;
}

/**
 * Removes a single instance of the specified element from this
 * collection, if it is present (optional operation).
 *
 * @param element to be removed from this collection, if present.
 * @see	java.util.Collection#add(Object)
 *
 */
public boolean removeEspaceTravail(EspaceTravail espaceTravail){
	return this.espaceTravail.remove(espaceTravail);
}

/**
 * Removes all of the elements from this collection (optional operation).
 *
 * @see	java.util.Collection#clear()
 *
 */
public void clearEspaceTravail(){
	this.espaceTravail.clear();
}



public void saveDB(BDConnect bdc)
{
	String sql="insert into "+Tables.DB_TABLE_CTLE +"("+CTLE.CHP_DAT_CTL+","+
	CTLE.CHP_MAT_CLEU+", "+CTLE.CHP_TRAIT+")"+
	"values('"+dateControle+"',"+this.getControleur().getIdEmploye()+","
	+Commons.DB_CTL_NON_TRA+")";
	//functions.affiche(sql);
	try {
		bdc.update(sql);
		this.setIdControle(BDFunctions.getLastID(Tables.DB_TABLE_CTLE, CTLE.CHP_ID, bdc));
		for(EspaceTravail et:this.getEspaceTravail())
		{
			//Save controleEspaceItems
			sql="insert into "+Tables.DB_TABLE_CTL_ESP+" values("+et.getIdEspaceTravail()
			+","+this.getIdControle()+")";
			bdc.update(sql);
			//functions.affiche(sql);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		//e.printStackTrace();
		functions.affiche("core controle insertion problem: "+e.getMessage());
	}
	
}

/**
 * @return the idControle
 */
public int getIdControle() {
	return idControle;
}

/**
 * @param idControle the idControle to set
 */
public void setIdControle(int idControle) {
	this.idControle = idControle;
}

/**
 * @return the dateControle
 */
public Date getDateControle() {
	return dateControle;
}

/**
 * @param dateControle the dateControle to set
 */
public void setDateControle(Date dateControle) {
	this.dateControle = dateControle;
}
}
